<?php
class News_Controller extends Base_Controller
{
    public $restful = true;

    public function __construct()
    {
        parent::__construct();
        $this->filter('before', 'auth')->except(array(
            'index',
            'view'
        ));
    }

    public function get_index()
    {
        $quantity = 5;
        $articles = News::order_by('display_order', 'asc')->order_by('updated_at', 'desc')->paginate($quantity);

        return View::make('news.index')
            ->with('rss', get_random_rss_news(6))
            ->with('quantity', $quantity)
            ->with('articles', $articles->results)
            ->with('pagination', $articles->speaks('vi')->links(2))
            ->with('title', 'Tin tức');
    }

    public function get_view($id, $slug = null)
    {
        $article = News::find($id);
        if (is_null($article)) {
            return message_page(array(
                'content' => 'Tin tức yêu cầu không hợp lệ',
                'type' => 'error',
                'title' => 'Lỗi truy cập'
            ));
        }

        $rss = get_random_rss_news(6);
        $title = $article->title;
        $article->increment('news_view');

        return View::make('news.view', compact('article', 'rss', 'title'));
    }

    public function get_post()
    {
        $title = "Đăng tin tức";
        return View::make('news.post', compact('title'));
    }

    public function post_post()
    {
        $rules = array(
            'title' => 'required|min:4|max:128',
            'brief' => 'required|min:10|max:256',
            'body' => 'required|min:20|max:10240',
        );

        $data = Input::get();

        if (Input::has_file('thumbnail') && !preg_match('/^https?:\/\//', $data['thumblink']) ) {
            $filename = time() . '-' . Input::file('thumbnail.name');
            $data['thumblink'] = $filename;
            Input::upload('thumbnail', 'public/img/thumbs', $filename);
        }

        $validator = Validator::make(Input::all(), $rules);

        if ($validator->fails()) {
            return Redirect::to('news/post')->with_input()->with_errors($validator);
        } else {

            $article = MyAuth::user()->news()->insert(array(
                'title' => $data['title'],
                'thumbnail' => $data['thumblink'],
                'brief' => $data['brief'],
                'body' => $data['body'],
                'display_order' => 11
            ));
            return Redirect::to($article->friendly_url);
        }
    }

    public function get_del($id)
    {
        $article = News::find($id);
        if (!is_null($article)) {
            return message_page(array(
                'content' => 'Bạn muốn xóa tin tức [' . $article->title . '] ?',
                'confirm_submit_url' => 'news/del/' . $id,
                'confirm_cancel_url' => 'tin-tuc',
                'confirm_submit_label' => 'Xác nhận xóa',
                'type' => 'warning'
            ));
        }
    }

    public function post_del($id)
    {
        News::find($id)->delete();
        if (Request::ajax()) {
            return Response::json(array('code' => 0));
        } else {
            return Redirect::to('tin-tuc');
        }
    }

    public function get_edit($id)
    {
        $article = News::find($id);
        $action = asset("news/edit/$id");

        if (is_not_null($article)) {
            $view = View::make('news.post', compact('article', 'action'));
        } else {
            $view = message_page(array(
                'Yêu cầu cập nhật tin tức không hợp lệ!',
                'type' => 'error'
            ));
        }

        return $view;
    }

    public function post_edit($id)
    {
        $article = News::find($id);
        if (is_null($article)) {
            return Response::error(404);
        }

        $rules = array(
            'title' => 'required|min:4|max:128',
            'brief' => 'required|min:10|max:256',
            'body' => 'required|min:20|max:10240',
        );

        $input = Input::get();

        if (Input::has_file('thumbnail') && !preg_match('/^https?:\/\//', $input['thumblink']) ) {
            $filename = time() . '-' . Input::file('thumbnail.name');
            $input['thumblink'] = $filename;
            Input::upload('thumbnail', 'public/img/thumbs', $filename);
        }

        $validator = Validator::make(Input::all(), $rules);

        if ($validator->fails()) {
            return Redirect::to(action('news@edit', array($id)))->with_input()->with_errors($validator);
        } else {

            $article->title = $input['title'];
            $article->thumbnail = $input['thumblink'];
            $article->brief = $input['brief'];
            $article->body = $input['body'];

            $article->save();
            return Redirect::to($article->friendly_url);
        }
    }
}